% Excercise 5

load('mkl_data.mat');

% Variables
testSize = 150;
test = randperm(300,testSize); % choosing half of the data for testing
train = 1:length(class) ;
train(test) = [];


output1  = sign(svm(class(train),K1(train,train),K1(test,train),1)); % model based on first kernel 1(model 1)
error1 = sum(output1~=class(test)) / testSize; % error rate of model 1

output2  = sign(svm(class(train),K2(train,train),K2(test,train),1)); % model based on first kernel 2(model 2)
error2 = sum(output2~=class(test)) / testSize; % error rate of model 2

output3  = sign(svm(class(train),K3(train,train),K3(test,train),1)); % model based on first kernel 3(model 3)
error3 = sum(output3~=class(test)) / testSize; % error rate of model 3

output4  = sign(svm(class(train),K4(train,train),K4(test,train),1)); % model based on first kernel 4(model 4)
error4 = sum(output4~=class(test)) / testSize; % error rate of model 4

K_UniformWeight = (K1 + K2 + K3 + K4 )./ 4;
outputUniformWeight  = sign(svm(class(train),K_UniformWeight(train,train),K_UniformWeight(test,train),1)); % model based on linear combination of kernels with unifrom weights(model 5)
errorUniformWeight  = sum(outputUniformWeight~=class(test)) / testSize; % error rate of model 5



% creating coefficients for combining kernels (inverse of error rate)
sumOfError = 1/error4 + 1/error3 + 1/error2 + 1/error1;
w1 = (1/error1)/sumOfError;
w2 = (1/error2)/sumOfError;
w3 = (1/error3)/sumOfError;
w4 = (1/error4)/sumOfError;

K_Weighted =  w1 * K1 + w2 * K2 + w3 * K3 + w4 * K4; % linear combination of error rates
outputWeighted  = sign(svm(class(train),K_Weighted(train,train),K_Weighted(test,train),1)); % model based on linear combination of kernels with different weights(model 6)
errorWeighted= sum(outputWeighted~=class(test)) / testSize; % error rate of model 6





error1
error2
error3
error4

errorUniformWeight

errorWeighted




